// pages/basicInfo/basicInfo.js
import { request } from "../../utils/index.js";
import { areaList} from "../../utils/areaList.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    minDate: new Date((new Date().getFullYear() - 70), 10, 1).getTime(),
    currentDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    realname:'', // 姓名
    genderList:['男','女'], // 性别列表
    gender:1, // 性别
    genderText:'', // 性别
    birthday:"", // 生日
    areaList:{},  // 选中省市区数据
    province_id:"", // 省id 
    city_id:"", // 市id 
    district_id:"", // 区id
    province:"", // 省
    city:"", // 市
    district:"", // 区
    qualificationList:["初中","高中/中专","本科/大专","硕士","博士"],
    qualification:1, // 学历
    qualificationText:"", // 学历
    major:"", // 专业
    height:0, // 身高
    weight:0, // 体重
    type:0 // 弹窗类型
  },

  // 输入姓名
  onChangeName(event){
    this.setData({realname:event.detail})
  },

  // 输入专业
  onChangeMajor(event){
    this.setData({major:event.detail})
  },

  // 输入身高
  onChangeHeight(event){
    this.setData({height:event.detail})
  },

  // 输入体重
  onChangeWeight(event){
    this.setData({weight:event.detail})
  },

  // 弹窗
  showPopup(event) {
    console.log(event)
    var type = event.currentTarget.dataset.index;
    this.setData({type:type,show: true});
  },

  onClose() {
    this.setData({ show: false });
  },

  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },

   // 时间戳转时间字符串
   timeFormat(date, fmt) {
    var o = {
      "M+": date.getMonth() + 1,                 //月份   
      "d+": date.getDate(),                    //日
      "h+": date.getHours(),                   //小时   
      "m+": date.getMinutes(),                 //分   
      "s+": date.getSeconds(),                 //秒   
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
      "S": date.getMilliseconds()             //毫秒   
    };
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
   
  },


   // 点击确定
   onConfirm(event) {
    if (this.data.type == 0) { // 选择性别
      const { value, index } = event.detail;
      this.setData({
        show:false,
        gender:index+1,
        genderText:value
      }) 
    }else if (this.data.type == 1) { // 选择生日
      var timeValue = this.timeFormat(new Date(event.detail), "yyyy-MM-dd");
      console.log(timeValue)
      this.setData({
        show:false,
        birthday:timeValue,
      })
      
    }else if (this.data.type == 2) { // 选择地址
      console.log(event);
      this.setData({ show: false });
      var that = this;
      if (event.detail.values.length > 0) {
        for (let i = 0; i < event.detail.values.length; i++) {
          if(i == 0 ){
            var province_id = event.detail.values[0].code;
            var province = event.detail.values[0].name;
            that.setData({
              province_id:province_id,
              province:province,
            })
          }else if (i == 1) {
            var city_id = event.detail.values[1].code;
            var city = event.detail.values[1].name;
            that.setData({
              city_id:city_id,
              city:city
            })
          }else {
            var district_id = event.detail.values[2].code;
            var district = event.detail.values[2].name;
            that.setData({
              district_id:district_id,
              district:district
            })
          }
        }
      }
    }else if (this.data.type == 3) { // 选择学历
      const { value, index } = event.detail;
      this.setData({
        show:false,
        qualification:index+1,
        qualificationText:value
      }) 
    }
  },


  // 点击保存
  onSubmit(){
    var that = this; 
    request({ url: "/api/resume/update",data:{
      realname:that.data.realname,
      gender:that.data.gender,
      birthday:that.data.birthday,
      province_id:that.data.province_id,
      city_id:that.data.city_id,
      district_id:that.data.district_id,
      qualification:that.data.qualification,
      major:that.data.major,
      height:that.data.height,
      weight:that.data.weight,
    }})
    .then(result => {
      wx.showToast({
        title: result.msg,
        icon: 'none',
        duration:2000
      });
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({areaList:areaList})
    if (options.jsonStr) {
      let item=JSON.parse(decodeURIComponent(options.jsonStr));
      this.setData({
        realname:item.realname,
        gender:item.gender,
        genderText:item.gender==1?'男':'女',
        birthday:item.birthday,
        province:item.province_id_text,
        city:item.city_id_text,
        district:item.district_id_text,
        qualification:item.qualification,
        qualificationText:item.qualification==1?'初中':item.qualification==2?'高中/中专':item.qualification==3?'本科/大专':item.qualification==4?'硕士':item.qualification==5?'博士':"",
        major:item.major,
        height:item.height,
        weight:item.weight,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})